package org.originit.factory.config;

import com.sun.istack.internal.NotNull;

import java.util.List;

public class BeanDefinition {

    private Object instance;

    private Class<?> type;

    private String name;

    private PropertyValues propertyValues;

    public BeanDefinition (@NotNull Class<?> type,PropertyValues propertyValues) {
        this.type = type;
        this.name = getDefaultName(type);
        this.propertyValues = propertyValues == null?new PropertyValues():propertyValues;
    }

    public BeanDefinition (@NotNull Class<?> type) {
       this(type,null);
    }

    private String getDefaultName(Class<?> type) {
        char[] chars = type.getSimpleName().toCharArray();
        // 首字母小写
        if (chars[0] <= 'Z' && chars[0] >= 'A') {
            chars[0] = (char) (chars[0] + 32);
        }
        return String.valueOf(chars);
    }

    public List<PropertyValue> getPropertyValues() {
        return propertyValues.getPropertyValues();
    }

    public Object getBean() {
        if (instance == null) {
           throw new RuntimeException("bean还没有创建");
        }
        return instance;
    }

    public Class<?> getType() {
        return this.type;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
